package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.wx.WxCouponService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxCouponUserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    SecurityManager securityManager;

    @GetMapping("list")
    public Object list(@RequestParam(required = false, defaultValue = "1") Integer page,
                       @RequestParam(required = false, defaultValue = "10") Integer limit){
        List<MarketCoupon> marketCouponList = wxCouponService.list();
        return ResponseUtil.okList(marketCouponList);
    }

    @GetMapping("mylist")
    public Object mylist(  @RequestParam(required = false) Short status,
                         @RequestParam(required = false, defaultValue = "1") Integer page,
                         @RequestParam(required = false, defaultValue = "10") Integer limit){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        Integer userId = user.getId();
        List<WxCouponUserVo> wxCouponUserVoList = wxCouponService.mylist(userId,status);
        return ResponseUtil.okList(wxCouponUserVoList);
    }

    //GET /wx/coupon/selectlist?cartId=0&grouponRulesId=0 HTTP/1.1
    @GetMapping("selectlist")
    public Object selectlist(  @RequestParam(required = false) Integer cartId){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        Integer userId = user.getId();
        List<WxCouponUserVo> wxCouponUserVoList = wxCouponService.selectlist(cartId,userId);
        return ResponseUtil.okList(wxCouponUserVoList);
    }

    @PostMapping("receive")
    public Object receive(@RequestBody Map<String, Object> requestBody){
        Integer couponId = (Integer) requestBody.get("couponId");
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        Integer userId = user.getId();
        Integer result = wxCouponService.receive(userId,couponId);
        if(result == 0){
            return ResponseUtil.ok();
        } else if (result == 1) {
            return ResponseUtil.fail(740,"优惠券已经领取过");
        }
        return ResponseUtil.fail(600,"领取失败");

    }

    @PostMapping("exchange")
    public Object exchange(@RequestBody Map<String, Object> requestBody){
        String code = (String) requestBody.get("code");
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        Integer userId = user.getId();
        Integer result = wxCouponService.exchange(userId,code);
        if(result == 0){
            return ResponseUtil.ok();
        } else if (result == 1) {
            return ResponseUtil.fail(740,"优惠券已兑换");
        }
        return ResponseUtil.fail(742,"优惠券不正确");
    }
}
